package yxy.game.pm2.active.command.weeklycard;

import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actweekcarkgrow;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_周卡;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.ActiveTask;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyActiveTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/**
 * 周卡_目标任务 信息
 * @author Dell
 *
 */
@MessageType(proto = Command.ActWeeklyCardActiveInfo, classtype = ActiveMessage.ActWeeklyCardActiveInfoRequest.class)
public class CMD_Active_周卡_活跃任务_Info extends Command<ActiveMessage.ActWeeklyCardActiveInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_周卡_活跃任务_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActWeeklyCardActiveInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActWeeklyCardActiveInfoResponse.Builder response = ActiveMessage.ActWeeklyCardActiveInfoResponse.newBuilder();
		
		ActiveModule_周卡 am = ActiveModule_周卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		WeeklyActiveTask wat = am.getWat();

		int keynum = wat.getKeynum();
		response.setKeynum(keynum);
		
		//宝箱
		Map<String, actweekcarkgrow> awcgmap = ActDataCenter.getAllData(actweekcarkgrow.class, playercode);

//		long point = Point.get(playercode, CurrencyType.CT_周卡_货币2);
		for(int id : wat.getGrowids()) {
			ActiveMessage.WeeklyCardActiveBox.Builder build = ActiveMessage.WeeklyCardActiveBox.newBuilder();
			build.setId(id);
			
			actweekcarkgrow data = awcgmap.get(String.valueOf(id));
			build.setCondition(data.getCondition());

			Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
			int canget = wat.getGrowgetids().contains(id) ? DrawStatus.hadget.getStatus() : keynum > data.getCondition() ?
					DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
			
			response.addWcabs(build.build());
 		}
	
		//任务
//		Map<String, actweekcarktask> awctmap = DataCenter.getAllData(actweekcarktask.class);
		for(ActiveTask at : wat.getAtmap().values()) {
			ActiveMessage.WeeklyCardActiveTask.Builder build = ActiveMessage.WeeklyCardActiveTask.newBuilder();
			build.setId(at.getId());
			build.setTaskid(at.getTaskid());
			
			task taskdata = DataCenter.getData(at.getTaskid(), task.class);
			if(taskdata == null) {
				continue;
			}
			Collection<Currency> curs = Currency.create(taskdata.getRewardtype(), taskdata.getRewardid(), taskdata.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
			Task taskbean = Task.get(playercode, at.getTaskcode());
			if(taskbean == null) {
				continue;
			}
			int canget = taskbean.isReward() ? DrawStatus.hadget.getStatus() : taskbean.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
			build.setProgress(taskbean.getProgress());
			
			response.addWcats(build.build());
		}
		
		response.setEnd(am.getTimeline().getEndTime().getMillis());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWeeklyCardActiveInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
